package com.zrc.superframe_aop.aspect

import android.view.View
import com.zrc.superframe_aop.SuperAop
import com.zrc.superframe_aop.annotation.SingleClick
import com.zrc.superframe_aop.utils.ClickUtils.isFastDoubleClick
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Pointcut

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Descrip:  防止View被连续点击
 */
@Aspect
class SingleClickAspectJ : FatherAspect() {

    //方法切入点
    @Pointcut("execution(@com.zrc.superframe_aop.annotation.SingleClick * *(..))")
    fun singleClickPointcut() {
    }

    @Around("singleClickPointcut() && @annotation(singleClick)") //在连接点进行方法替换
    @Throws(Throwable::class)
    fun aroundJoinPoint(joinPoint: ProceedingJoinPoint, singleClick: SingleClick) {
        var view: View? = null
        for (arg in joinPoint.args) {
            if (arg is View) {
                view = arg
                break
            }
        }
        if (view != null) {
            if (SuperAop.isInit()) {
                if (!isFastDoubleClick(view, singleClick.value)) {
                    joinPoint.proceed() //不是快速点击，执行原方法
                } else {
                    logger("连续点击，本次aop拦截,view id:${view.id}")
                }
            } else {
                joinPoint.proceed()
                logger("SuperAop未初始化，防止连续点击不生效")
            }
        }
    }
}